/*
 * AuthorPanelSubmit.java
 * Author: Tyler Simrell
 * Edited by: Aaron Kaufman
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Conference;
import model.ConferencePaper;

@SuppressWarnings("serial")
public class AuthorPanelSubmit extends JPanel implements ActionListener 
{
	String my_name;
	Conference my_conference;
	JTextField titleField;
	JTextField authorField;
	JTextField emailField;
	JTextField categoryOfPaperField;
	JTextField keywordsField;
	JTextField abstractField;
	JTextField filePath;
	JButton openButton;
	JFileChooser fileChooser;
	JButton submitButton;
	
	public AuthorPanelSubmit(String the_user, Conference the_conference)
	{
		super(new BorderLayout());
		my_name = the_user;
		my_conference = the_conference;
		setUpPanel();
	}
	
	//This method creates the components for the submit panel. This includes all of the labels and
	//the components that allow for the user to upload their file.
	private void setUpPanel()
	{
		this.add(new JLabel("<html><body>Please enter in the correct information for your paper.<br> " +
				"Note that blank fields will be entered as blank fields, so please make sure<br>" +
				"that if you want to have the correct information, inputted it here!" +
				"</body></html>"), BorderLayout.NORTH);
		submitButton = new JButton("Submit");
		JPanel informationPanel = new JPanel(new GridLayout(2,1));
		this.add(informationPanel, BorderLayout.CENTER);
		
		JPanel fields = new JPanel(new GridLayout(0,2)); //Allows for having the seventh field simply be a label
		                                                 //for the text area.
		fields.add(new JLabel("Enter Your Paper's Title Here [100 character max]:"));
		titleField = new JTextField();
		fields.add(titleField);
		
		//fields.add(new JLabel("Enter The Author's Name:"));
		//authorField = new JTextField();
		//fields.add(authorField);
		
		fields.add(new JLabel("Enter your email here:"));
		emailField = new JTextField();
		fields.add(emailField);
		
		fields.add(new JLabel("Enter the Category that Your Paper Fits Into:"));
		categoryOfPaperField = new JTextField();
		fields.add(categoryOfPaperField);
		
		fields.add(new JLabel("Enter the Keywords that apply to your Paper:"));
		keywordsField = new JTextField();
		fields.add(keywordsField);
		
		fields.add(new JLabel("Enter the abstract for your paper [100 words max]:"));
		abstractField = new JTextField();
		fields.add(abstractField);
		
		fields.add(new JLabel("Your Manuscript File Path"));
		
		JPanel fileUploaderPanel = new JPanel(new GridLayout(2,1));
		
		filePath = new JTextField();
		JPanel panelWrapper = new JPanel();
		filePath.setPreferredSize(new Dimension(600, 30));
		panelWrapper.add(filePath);
		fileUploaderPanel.add(panelWrapper);
		openButton = new JButton ("Open...");
		openButton.addActionListener(this);
		fileChooser = new JFileChooser();
		openButton.setPreferredSize(new Dimension(100, 20));
		panelWrapper = new JPanel();
		panelWrapper.add(openButton);
		fileUploaderPanel.add(panelWrapper);
		
		informationPanel.add(fields);
		informationPanel.add(fileUploaderPanel);
		panelWrapper = new JPanel();
		submitButton.addActionListener(this);
		panelWrapper.add(submitButton);
		this.add(panelWrapper, BorderLayout.SOUTH);
	}
	
		//This method copies the file into the file directory by using the FileInputStream
		//and the FileOutputStream <Java API is quite helpful here>. 
		private File copyFile(File the_input_file)
		{
			File newFile = null;
			FileInputStream input;
			try 
			{
				input = new FileInputStream(the_input_file);
				newFile = new File("Manuscripts/" , the_input_file.getName());
				FileOutputStream output = new FileOutputStream(newFile);
				byte[] stream = new byte[input.available()];
				input.read(stream);
				output.write(stream);
				output.close();
				input.close();
			}
			catch (FileNotFoundException e) 
			{
				System.err.println("File not found. Whoops!");
			} 
			
			catch (IOException e) 
			{
				System.err.println("IO Exception");
			}
			
			return newFile;
		}
	
	//This actionPerformed method is attached to the submit button and takes all the text taken from the
	//text fields/areas.
	//Edited by Aaron Kaufman
	@Override
	public void actionPerformed(ActionEvent the_event) 
	{
		if(the_event.getSource() == openButton)
		{
			int userChoice = fileChooser.showOpenDialog(null);
			if(userChoice == JFileChooser.APPROVE_OPTION)
			{
				File file = fileChooser.getSelectedFile();
				filePath.setText(file.getAbsolutePath());
			}
		}
		
		else if(the_event.getSource() == submitButton)
		{
			ArrayList<ConferencePaper> papers = new ArrayList<ConferencePaper>();
			for(ConferencePaper p : my_conference.getProgramChair().getPapers())
			{
				if(my_name.equals(p.getAuthorName()))
				{
					papers.add(p);
				}
			}
			//This check makes sure that an author can't submit more than 4 papers.
			if(papers.size() < 4)
			{
				if("".equals(filePath.getText()))
				{
					JOptionPane.showMessageDialog(null, "You Have To Specify Where Your File Is.", 
							                      "ERROR", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					File sentInFile = new File(filePath.getText());
					filePath.setText(sentInFile.getAbsolutePath());
					File conferenceFilePath = null;
					if(sentInFile.exists())
					{
							conferenceFilePath = copyFile(sentInFile);
					}
					
					boolean contained = false;
					for(ConferencePaper temp : my_conference.getProgramChair().getPapers())
					{
						if(titleField.getText().equals(temp.getTitle())){
							contained = true;
						}
					}
					
					if(!contained)
					{
						my_conference.addPaperToConference(
							my_name, titleField.getText(), 
							conferenceFilePath.getAbsolutePath(), my_name, 
							emailField.getText(), categoryOfPaperField.getText(),
							keywordsField.getText(), abstractField.getText());
						JOptionPane.showMessageDialog(null, "Manuscript Successfully Submitted");
					}
					else
					{
						JOptionPane.showMessageDialog(null, "A Paper with the same title has already " +
								"been submitted", "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
			
			else
			{
				JOptionPane.showMessageDialog(null, "You May Only Submit 4 Papers", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
